package com.moon.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moon.dao.TAuctionItemMapper;
import com.moon.dao.TAuctionMapper;
import com.moon.dao.TAuctionVoMapper;
import com.moon.dao.THistoryMapper;
import com.moon.entity.TAuction;
import com.moon.entity.TAuctionItem;
import com.moon.entity.THistory;
import com.moon.entity.TUser;
import com.moon.utils.UserUtils;
import com.moon.vo.TAuctionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/tauction")
@Controller
public class TAuctionController {

    @Autowired
    private TAuctionVoMapper tAuctionVoMapper;
    @Autowired
    private TAuctionItemMapper tAuctionItemMapper;
    @Autowired
    private THistoryMapper tHistoryMapper;

    //查询我创建的竞拍室
    @RequestMapping("/selectMyTAuction")
    @ResponseBody
    public PageInfo<TAuctionVo> selectMyTAuction(@RequestParam(defaultValue = "1") Integer currentPage,
                                                 @RequestParam(defaultValue = "1")Integer pageSize,
                                                 @RequestParam(defaultValue = "")Integer keyword,
                                                 HttpServletRequest req) {
        System.out.println("---TA_selectMyTAuction---");
        System.out.println("currentPage: "+currentPage+" pageSize: "+pageSize);
        TUser user = UserUtils.getCurrUserFromHeader("token", TUser.class, req);
        PageHelper.startPage(currentPage,pageSize);
        List<TAuctionVo> tAuctions = tAuctionVoMapper.selectTAuctionVoBySellerId(user.getId());
        PageInfo<TAuctionVo> pageInfo = new PageInfo<>(tAuctions);
        return pageInfo;
    }

    //查询我参加的竞拍室
    @RequestMapping("/selectParticipateTAuctions")
    @ResponseBody
    public PageInfo<TAuctionVo> selectParticipateTAuction(@RequestParam(defaultValue = "1") Integer currentPage,
                                                          @RequestParam(defaultValue = "1")Integer pageSize,
                                                          @RequestParam(defaultValue = "")Integer keyword,
                                                          HttpServletRequest req) {
        System.out.println("---TA_selectParticipateTAuction---");
        System.out.println("currentPage: "+currentPage+" pageSize: "+pageSize);
        //fixme 临时userId
//        TUser user = UserUtils.getCurrUserFromHeader("token", TUser.class, req);
        Long userId = 410673088817270784L;

        PageHelper.startPage(currentPage,pageSize);
        //获取用户参加过的竞拍记录
        List<THistory> items = tHistoryMapper.selectTHistoryByUserId(userId);
        //遍历竞拍记录,获取竞拍室List
        List<TAuctionVo> tAuctionVos = new ArrayList<>();
        for (THistory item : items) {
            TAuctionVo tAuctionVo = tAuctionVoMapper.selectById(item.getAuctionId());
            tAuctionVos.add(tAuctionVo);
        }
        PageInfo<TAuctionVo> pageInfo = new PageInfo<>(tAuctionVos);
        return pageInfo;
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public TAuctionVo selectById(Long id) {
        return tAuctionVoMapper.selectById(id);
    }

}
